package com.weetop.cfw.mine.activity

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.blankj.utilcode.util.ActivityUtils
import com.weetop.cfw.R
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.ui.fragment.CommonBaseFragment
import com.weetop.cfw.mine.fragment.AllCoursesFragment
import com.weetop.cfw.mine.fragment.SuccessfulPaymentCoursesFragment
import com.weetop.cfw.mine.fragment.ToBePaidCoursesFragment
import kotlinx.android.synthetic.main.activity_my_courses.*


/**
 * 我的课程界面
 */
class MyCoursesActivity : CommonBaseActivity(),View.OnClickListener {

    private val allCoursesFragment = AllCoursesFragment()
    private val toBePaidCoursesFragment = ToBePaidCoursesFragment()
    private val successfulPaymentCoursesFragment = SuccessfulPaymentCoursesFragment()

    private val courseListFragment = listOf<CommonBaseFragment>(allCoursesFragment,toBePaidCoursesFragment,successfulPaymentCoursesFragment)

    override fun getLayoutId() = R.layout.activity_my_courses

    override fun initView(savedInstanceState: Bundle?) {
        setViewsOnClickListener(this,imageMyCoursesBack)

        initCourseTab()
    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    /**
     * 初始化课程Tab
     */
    private fun initCourseTab(){

        val courseTitleList = listOf("全部课程","待支付","支付成功")

        val coursePagerAdapter = MyCoursePagerAdapter(supportFragmentManager,courseListFragment,courseTitleList)
        myCourseViewPager.adapter = coursePagerAdapter
        myCourseViewPager.offscreenPageLimit=3
        myCoursesTab.setupWithViewPager(myCourseViewPager)

    }

    override fun onClick(p0: View?) {
       when(p0?.id){
           R.id.imageMyCoursesBack -> ActivityUtils.finishActivity(this)
       }
    }

    private class MyCoursePagerAdapter(fragmentManager:FragmentManager, var courseFragmentList:List<CommonBaseFragment>, var titleList:List<String>):FragmentStatePagerAdapter(fragmentManager){

        override fun getItem(position: Int) = courseFragmentList[position]

        override fun getCount() = courseFragmentList.size

        override fun destroyItem(container:ViewGroup, position: Int, `object`: Any) {
//            super.destroyItem(container, position, `object`)
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return titleList[position]
        }

    }
}
